package com.kma.summer2012.client;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.googlecode.gwtTableToExcel.client.TableToExcelClient;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.client.constants.ConstantsPresenter;
import com.kma.summer2012.shared.model.UICrumbsClient;
import com.kma.summer2012.shared.model.UICrumbsDateAndTime;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 2/27/13
 * Time: 11:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExportManager {
    private ExportManager() {
    }

    private ConstantsPresenter c = ConstantsHolder.getConstants();
    private static ExportManager myself = null;

    public static ExportManager getInstance() {
        if (myself == null) {
            myself = new ExportManager();
        }
        return myself;
    }

    public FormPanel go(List<UICrumbsRequest> requests) {
        TableToExcelClient tableToExcelClient = new TableToExcelClient(getRequestsTable(requests), new Button("Submit"));
        return (tableToExcelClient.getExportWidget());
    }

    private Grid getRequestsTable(List<UICrumbsRequest> requests) {
        Grid grid = new Grid(1, 3);
        grid.setTitle("Exported requests");
        int lastRow = 0;
        for (UICrumbsRequest request : requests) {
            lastRow = addRequestInfoToGrid(grid, request, lastRow);
        }
        return grid;
    }

    private int addRequestInfoToGrid(Grid grid, UICrumbsRequest request, int row) {
        grid.insertRow(row);
        grid.setText(row++, 0, "Request export");
        grid.insertRow(row);
        grid.setText(row, 0, "Id");
        grid.setText(row++, 1, String.valueOf(request.getId()));
        grid.insertRow(row);
        grid.setText(row, 0, "Date of creation");
        grid.setText(row++, 1, "[" + request.getDateOfCreation() + "]");
        grid.insertRow(row);
        grid.setText(row, 0, "State");
        grid.setText(row++, 1, request.getState());
        grid.insertRow(row);
        grid.setText(row, 0, "Request info");
        grid.setText(row++, 1, request.getRequestInfo());
        grid.insertRow(row);
        grid.setText(row++, 0, "Type of work:");
        grid.insertRow(row);
        Map<UICrumbsWorkItem, Short> works = request.getUicrumbsWorkItems();
        grid.insertRow(row);
        if (works != null) {
            Set<UICrumbsWorkItem> workItemSet = works.keySet();
            int i = 1;
            for (UICrumbsWorkItem item : workItemSet) {
                grid.insertRow(row);
                grid.setText(row, 0, String.valueOf(i++));
                grid.setText(row++, 1, item.getWorkName());
            }
        }
        grid.insertRow(row);
        if (request.getUicrumbsClient() != null) {
            UICrumbsClient client = request.getUicrumbsClient();
            grid.setText(row++, 0, "Client`s Info");
            grid.insertRow(row);
            grid.setText(row, 1, "Name");
            grid.setText(row++, 2, client.getName());
            grid.insertRow(row);
            grid.setText(row, 1, "Address");
            grid.setText(row++, 2, client.getAdress());
            grid.insertRow(row);
            grid.setText(row, 1, "Mail");
            grid.setText(row++, 2, client.getMail());
        }
        grid.insertRow(row);
        grid.setText(row++, 0, "Requested time");
        Set<UICrumbsDateAndTime> dateAndTime = request.getUicrumbsDateAndTimes();
        if (dateAndTime != null) {
            for (UICrumbsDateAndTime item : dateAndTime) {
                grid.insertRow(row);
                String timeBegin = String.valueOf(DateTimeFormat.getFormat(c.DATE_FORMAT()).format(item.getDateBegin()));
                String timeEnd = String.valueOf(DateTimeFormat.getFormat(c.DATE_FORMAT()).format(item.getDateEnd()));
                grid.setText(row, 1, "[" + timeBegin + "]");
                grid.setText(row++, 2, "[" + timeEnd + "]");
            }
        }
        grid.insertRow(row);
        grid.setText(row++, 0, "Assigned time");
        Set<UICrumbsDateAndTime> dateAndTimeAssigned = request.getUicrumbsDateAndTimes();
        for (UICrumbsDateAndTime item : dateAndTimeAssigned) {
            if ((item.getAssignedTimeEnd() != null) && (item.getAssignedTimeBegin() != null)) {
                grid.insertRow(row);
                String timeBegin = String.valueOf(DateTimeFormat.getFormat(c.DATE_FORMAT()).format(item.getAssignedTimeBegin()));
                String timeEnd = String.valueOf(DateTimeFormat.getFormat(c.DATE_FORMAT()).format(item.getAssignedTimeEnd()));
                grid.setText(row, 1, "[" + timeBegin + "]");
                grid.setText(row++, 2, "[" + timeEnd + "]");
            }
        }
        grid.insertRow(row++);
        grid.insertRow(row++);
        return row;
    }
}
